#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "ListOfPoint.h"

/*
int main()
{
	ListOfPoint LOP;
	createList(&LOP);
	
	add(&LOP,1,2);
	add(&LOP,3,5);
	add(&LOP,6,8);
	add(&LOP,0,1);
	
	printf(" %d\n",isEmpty(LOP));
	printList(LOP);
	printf("getX,getY -> %d,%d\n",get(LOP,0).x,get(LOP,0).y);
	printf("size -> %d\n",size(LOP));
	clearList(&LOP);
	printList(LOP);
	printf("size -> %d\n",size(LOP));
}
*/

bool isEmpty(ListOfPoint LOP)
{
	return (LOP==NULL);
}

void printList(ListOfPoint LOP)
{
	if (!isEmpty(LOP)){
	ListOfPoint temp = LOP;
		while (temp != NULL){
			printf("x,y -> %d,%d\n",temp->x,temp->y);
			temp = temp->next;
		}
	}
}

void add(ListOfPoint* LOP, int x, int y)
{
	ListOfPoint temp = (ListOfPoint) malloc (1 * sizeof (Point));
	if ((*LOP) == NULL){
		temp->x = x;
		temp->y = y;
		temp->next = NULL;
		*LOP = temp;
	} else{
		ListOfPoint tempLOP = *LOP;
		while (tempLOP->next!=NULL){
			tempLOP = tempLOP->next;
		}
		temp->x = x;
		temp->y = y;
		temp->next = NULL;
		tempLOP->next = temp;	
	}
}

void del(ListOfPoint *LOP)
{
	ListOfPoint temp1, temp2;
	if ((*LOP)->next == NULL){
		temp1 = *LOP;
		*LOP = NULL;
		free(temp1);
	}else{
		temp1 = *LOP;
		temp2 = (*LOP)->next;
		while (temp2->next != NULL){
			temp1->x = temp2->x;
			temp1->y = temp2->y;
			temp1 = temp1->next;
			temp2 = temp2->next;
		}
		temp1->next = NULL;
		free(temp2);
	}
}

void createList(ListOfPoint* LOP)
{
	*LOP = NULL;
}

void clearList(ListOfPoint* LOP)
{
	ListOfPoint temp;
	ListOfPoint tempLOP;
	
	while (*LOP != NULL){
		tempLOP = *LOP;
		if (tempLOP->next==NULL){
			*LOP=NULL;
			free(tempLOP);
		}
		else{
			while(tempLOP->next->next!=NULL){
				tempLOP = tempLOP->next;
			}
			temp = tempLOP->next;
			tempLOP->next = NULL;
			free(temp);
		}
	}
}

Point get(ListOfPoint LOP, int idx)
{
	ListOfPoint tempLOP = LOP;
	while (idx!=0){
		tempLOP = tempLOP->next;
		idx--;		
	}
	return(*tempLOP);
}

ListOfPoint getAddress(ListOfPoint LOP, int idx)
{
	ListOfPoint tempLOP = LOP;
	while (idx!=0){
		tempLOP = tempLOP->next;
		idx--;		
	}
	return(tempLOP);
}

int getX(ListOfPoint LOP, int idx)
{
	return (get(LOP,idx).x);
}

int getY(ListOfPoint LOP, int idx)
{
	return (get(LOP,idx).y);
}

int sizeLOP(ListOfPoint LOP)
{
	int i = 1;
	ListOfPoint temp = LOP;
	if (LOP==NULL){return 0;}
	while (temp->next!=NULL){
		i++;
		temp = temp->next;
	}
	return i;
}
